package com.yanyeori.framework.i18n.util;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * i18n多语言解析工具类
 *
 * @author chenkuan 2022/7/18
 */
public class I18nUtil implements InitializingBean {

    private final MessageSource messageSource;

    private static I18nUtil instance;

    public I18nUtil(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        instance = this;
    }

    public static MessageSource getMessageSource() {
        return instance.messageSource;
    }

    public static String getMessage(String key, Object... objs) {
        return getMessageSource().getMessage(key, objs, LocaleContextHolder.getLocale());
    }
}
